package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"math"
	"news/models"
	"strings"
)

type NewsControllers struct {
	beego.Controller
}
type Json struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (n *NewsControllers) Add() {
	title := n.GetString("title")
	content := n.GetString("content")
	news := models.New{
		Title:   title,
		Content: content,
	}
	err := models.DB.Model(&models.New{}).Create(&news).Error
	if err != nil {
		n.Data["json"] = Json{
			Code: 500,
			Msg:  "添加失败",
			Data: nil,
		}
		n.ServeJSON()
	} else {
		n.Data["json"] = Json{
			Code: 200,
			Msg:  "添加成功",
			Data: news,
		}
		n.ServeJSON()
	}
}

func (n *NewsControllers) Del() {
	id, _ := n.GetInt("id")
	err := models.DB.Model(&models.New{}).Where("id=?", id).Delete(id).Error
	if err != nil {
		n.Data["json"] = Json{
			Code: 500,
			Msg:  "删除失败",
			Data: nil,
		}
		n.ServeJSON()
	} else {
		n.Data["json"] = Json{
			Code: 200,
			Msg:  "删除成功",
			Data: nil,
		}
		n.ServeJSON()
	}
}

func (n *NewsControllers) List() {
	var news []models.New
	// 从请求参数中获取标题搜索条件
	title := n.GetString("title")
	var query = models.DB
	// 如果标题不为空，则构建模糊查询
	if title != "" {
		title = "%" + strings.ToLower(title) + "%" //将标题转小写并添加%进行模糊匹配
		query = query.Where("LOWER(title) LIKE ?", title)
	}
	// 执行查询，并按 id 倒序排列
	err := query.Order("id DESC").Find(&news).Error //根据id倒序
	if err != nil {
		// 如果查询出错，返回 500 状态码和错误信息
		n.Data["json"] = Json{
			Code: 500,
			Msg:  "展示失败",
			Data: nil,
		}
		n.ServeJSON()
	} else {
		n.Data["json"] = Json{
			Code: 200,
			Msg:  "展示成功",
			Data: news,
		}
		n.ServeJSON()
	}
}

func (n *NewsControllers) Update() {
	id, _ := n.GetInt("id")
	title := n.GetString("title")
	content := n.GetString("content")
	news := models.New{
		Title:   title,
		Content: content,
	}
	err := models.DB.Model(&models.New{}).Where("id=?", id).Updates(news).Error //select *from 表名 set name ="" where id=1
	if err != nil {
		n.Data["json"] = Json{
			Code: 500,
			Msg:  "修改失败",
			Data: nil,
		}
		n.ServeJSON()
	} else {
		n.Data["json"] = Json{
			Code: 200,
			Msg:  "修改成功",
			Data: nil,
		}
		n.ServeJSON()
	}
}

func (n *NewsControllers) Page() {
	//1.总数
	var count int64
	title := n.GetString("title")
	query := models.DB.Model(models.New{})
	err := query.Count(&count).Error
	if title != "" {
		query = query.Where("LOWER(title) LIKE ?", "%"+strings.ToLower(title)+"%")
	}
	if err != nil {
		n.Data["json"] = Json{
			Code: 500,
			Msg:  "展示失败",
			Data: nil,
		}
		n.ServeJSON()
		return
	}
	//2.每条页数
	size, _ := n.GetInt("size", 3)
	//3.页数
	sum := int(math.Ceil(float64(count) / float64(size)))
	//4.当前页
	page, _ := n.GetInt("p", 1)
	//5.偏移量
	offset := (page - 1) * size
	var news []models.New
	err = query.Order("id DESC").Offset(offset).Limit(size).Find(&news).Error
	if err != nil {
		n.Data["json"] = Json{
			Code: 500,
			Msg:  "分页失败",
			Data: nil,
		}
		n.ServeJSON()
	} else {
		pageData := make(map[string]interface{})
		pageData["sum"] = sum
		pageData["news"] = news
		n.Data["json"] = Json{
			Code: 200,
			Msg:  "分页成功",
			Data: pageData,
		}
		n.ServeJSON()
	}

}
